/**
 *
 * @fileName    base
 * @author       Liu Yutao
 * @email        koal@vip.qq.com
 * @date         2016/2/24 14:01
 */


define([
    'jquery',
    'baseUtils',
    'plugins/pace/pace',
    'msgTool',
    'plugins/metisMenu/metisMenu',
    'plugins/slimscroll/jquery.slimscroll',
    'jqValidate',
    'jqValidateCN'
], function ($,baseUtils,pace, msgTool) {
    //加载进度条插件
    pace.start({
        document: false
    });
    //检查浏览器支持HTML5本地存储
    function localStorageSupport() {
        return (('localStorage' in window) && window['localStorage'] !== null)
    }

    function SmoothlyMenu() {
        if (!$('body').hasClass('mini-navbar') || $('body').hasClass('body-small')) {
            // 隐藏菜单为了顺利打开当最大化菜单
            $('#side-menu').hide();
            // 为顺利打开菜单
            setTimeout(
                function () {
                    $('#side-menu').fadeIn(500);
                }, 100);
        } else if ($('body').hasClass('fixed-sidebar')) {
            $('#side-menu').hide();
            setTimeout(
                function () {
                    $('#side-menu').fadeIn(500);
                }, 300);
        } else {
            // 删除所有内联风格从jquery渐显函数来设置菜单的状态
            $('#side-menu').removeAttr('style');
        }
    }


    // 自适应侧边栏的高度
    function fix_height() {
        var heightWithoutNavbar = $("body > #wrapper").height() - 61;
        $(".sidebard-panel").css("min-height", heightWithoutNavbar + "px");

        var navbarHeigh = $('nav.navbar-default').height();
        var wrapperHeigh = $('#page-wrapper').height();

        if (navbarHeigh > wrapperHeigh) {
            $('#page-wrapper').css("min-height", navbarHeigh + "px");
        }

        if (navbarHeigh < wrapperHeigh) {
            $('#page-wrapper').css("min-height", $(window).height() + "px");
        }

        if ($('body').hasClass('fixed-nav')) {
            $('#page-wrapper').css("min-height", $(window).height() - 60 + "px");
        }

    }

    // Metsi菜单
    $('#side-menu').metisMenu();


    return function () {
        // 添加body-small类,设置窗口小于768px
        if ($(this).width() < 769) {
            $('body').addClass('body-small')
        } else {
            $('body').removeClass('body-small')
        }
        // 关闭菜单在画布模式
        $('.close-canvas-menu').click(function () {
            $("body").toggleClass("mini-navbar");
            SmoothlyMenu();
        });

        // 打开关闭右栏
        $('.right-sidebar-toggle').click(function () {
            $('#right-sidebar').toggleClass('sidebar-open');
        });

        // 初始化slimscroll右栏
        //$('.ztree').slimScroll({
        //    height: '100%',
        //    railOpacity: 0.4,
        //    wheelStep: 10
        //});

        // 打开关闭小的聊天
        $('.open-small-chat').click(function () {
            $(this).children().toggleClass('fa-comments').toggleClass('fa-remove');
            $('.small-chat-box').toggleClass('active');
        });

        // 初始化slimscroll小聊天
        $('.small-chat-box .content').slimScroll({
            height: '234px',
            railOpacity: 0.4
        });

        // 小待办事项处理程序
        $('.check-link').click(function () {
            var button = $(this).find('i');
            var label = $(this).next('span');
            button.toggleClass('fa-check-square').toggleClass('fa-square-o');
            label.toggleClass('todo-completed');
            return false;
        });


        // 减少菜单
        $('.navbar-minimalize').click(function () {
            $("body").toggleClass("mini-navbar");
            SmoothlyMenu();

        });

        // 工具提示演示
        $('.tooltip-demo').tooltip({
            selector: "[data-toggle=tooltip]",
            container: "body"
        });


        $("[data-toggle=popover]").popover({
            html: true,
            trigger: "hover",
            placement: "top"
        });

        fix_height();

        // 修复工具条
        $(window).bind("load", function () {
            if ($("body").hasClass('fixed-sidebar')) {
                $('.sidebar-collapse').slimScroll({
                    height: '100%',
                    railOpacity: 0.9
                });
            }
        });

        // 减少菜单当屏幕小于768 px
        $(window).bind("resize", function () {
            if ($(this).width() < 769) {
                $('body').addClass('body-small')
            } else {
                $('body').removeClass('body-small')
            }
        });


        //设置基础消息弹窗参数
        msgTool.options = {
            "closeButton": true,
            "debug": false,
            "progressBar": true,
            "positionClass": "toast-top-right",
            "onclick": null,
            "showDuration": "400",
            "hideDuration": "1000",
            "timeOut": "3000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        };

        //表单验证  默认验证配置参数
        $.validator.setDefaults({
            highlight: function (e) {
                $(e).closest(".form-group>div").removeClass("has-success").addClass("has-error");
            },
            success: function (e) {
                e.closest(".form-group>div").removeClass("has-error").addClass("has-success")
            },
            errorElement: "span",
            errorPlacement: function (e, r) {
                e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
            },
            errorClass: "help-block m-b-none",
            validClass: "help-block m-b-none",
            //debug: false, //调试模式取消submit的默认提交功能
            //errorClass: "label.error", //默认为错误的样式类为：error
            focusInvalid: true, //当为false时，验证无效时，没有焦点响应
            onkeyup: false,
            submitHandler: function (form) { //表单提交句柄,为一回调函数，带一个参数：form
                // form.submit();   //提交表单
                // $(form).ajaxSubmit();
            },
        });

        // 用户昵称
        $.validator.addMethod("stringAllName", function (value, element) {
            return this.optional(element) || /^[a-zA-Z\u4e00-\u9fa5]{1}[a-zA-Z0-9-\u4e00-\u9fa5_]+$/m.test(value);
        }, "只能包括中文、英文、数字、下划线和分割线，数字和下划线不能开头");

        // 用户账号
        $.validator.addMethod("stringEnName", function (value, element) {
            return this.optional(element) || /^[a-zA-Z0-9]{1}[a-zA-Z0-9_-]+$/m.test(value);
        }, "只能包括英文、数字、下划线和分割线，分割线和下划线不能开头");


        // 包含中文
        $.validator.addMethod("stringZhCNName", function (value, element) {
            return this.optional(element) || /^[\u4e00-\u9fa5]+$/m.test(value);
        }, "不能包含中文字符");

        // 不能包含中文
        $.validator.addMethod("stringNoZhCNName", function (value, element) {
            return this.optional(element) || /^[^\u4e00-\u9fa5]+$/m.test(value);
        }, "不能包含中文字符");


        // 密码
        $.validator.addMethod("stringPassWord", function (value, element) {
            return this.optional(element) || /^[a-zA-Z0-9]{1}[a-zA-Z0-9_]+$/m.test(value);
        }, "只能包括英文、数字和下划线，下划线不能开头");

        // 手机号
        $.validator.addMethod("phone", function (value, element) {
            return this.optional(element) || /(13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7}/m.test(value);
        }, "只能为11位纯数字");

        if (localStorageSupport) {

            var collapse = localStorage.getItem("collapse_menu");
            var fixedsidebar = localStorage.getItem("fixedsidebar");
            var fixednavbar = localStorage.getItem("fixednavbar");
            var boxedlayout = localStorage.getItem("boxedlayout");
            var fixedfooter = localStorage.getItem("fixedfooter");

            var body = $('body');

            if (fixedsidebar === 'on') {
                body.addClass('fixed-sidebar');
                $('.sidebar-collapse').slimScroll({
                    height: '100%',
                    railOpacity: 0.9
                });
            }

            if (collapse === 'on') {
                if (body.hasClass('fixed-sidebar')) {
                    if (!body.hasClass('body-small')) {
                        body.addClass('mini-navbar');
                    }
                } else {
                    if (!body.hasClass('body-small')) {
                        body.addClass('mini-navbar');
                    }

                }
            }

            if (fixednavbar === 'on') {
                $(".navbar-static-top").removeClass('navbar-static-top').addClass('navbar-fixed-top');
                body.addClass('fixed-nav');
            }

            if (boxedlayout === 'on') {
                body.addClass('boxed-layout');
            }

            if (fixedfooter === 'on') {
                $(".footer").addClass('fixed');
            }
        }

    }

});




